<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

class SettingAction extends NewzanAction
{
	public function area()
	{
		$this->loadJS('jQuery.alimama','custom.appbar');
		$campaign_id = $this->_get('campaign_id');	
		$campaignMdl = new CampaignModel();
		$campaign_area = $campaignMdl->getCampaignArea($this->me['user_id'], $campaign_id , true);
				
		/*
		1．华北地区：北京、天津、河北、山西、内蒙古（5个省、市、区）。
        2．东北地区：辽宁、吉林、黑龙江、大连（4个省、市）。
        3．华东地区：上海、江苏、浙江、安徽、福建、江西、山东、宁波、夏门、青岛（10个省、市）。
        4．中南地区：河南、湖北、湖南、广东、广西、海南、深圳（7个省、市）。
        5．西南地区：重庆、四川、贵州、云南、西藏（5个省、市）。
        6．西北地区：陕西、甘肃、青海、宁夏、新疆（5个省、区）。
        */
		
		$defined_section = array(
		     '港澳台及海外' => '香港、澳门、台湾、国外',
		     '华北地区' => '北京、天津、河北、山西、内蒙古',
		     '东北地区' => '辽宁、吉林、黑龙江',
			 '华东地区' => '上海、江苏、浙江、安徽、福建、江西、山东',
			 '中南地区' => '河南、湖北、湖南、广东、广西、海南、深圳',
			 '西南地区' => '重庆、四川、贵州、云南、西藏',
			 '西北地区' => '陕西、甘肃、青海、宁夏、新疆'
		);
		
		$province_section = array();
		foreach($defined_section as $section => $provinces) {
		    $provinces = explode('、', $provinces);
		    foreach($provinces as $pro) {
		        $province_section[$pro] = $section;
		    } 
		}
		
		$section_area = array();
		foreach($campaign_area as $area){
		    $section = $province_section[ $area['name'] ];
		    $section_area[$section][] = $area;
		}
		$this->section_area = $section_area;
		$this->campaign_id = $campaign_id;
		$this->display();
	}
		
	public function savearea()
	{
		$area = $_POST['areas'];
		if(!$area) {
			$this->error('缺少参数,请重试');
		}
		$campaignMdl = new CampaignModel();
		$campaign_id = $_POST['campaign_id'];
		$area = array_unique($area);
		$area_options = $campaignMdl->getAreaOptions(2);
		//如果一个省的所有城市被选择了，那么这个省份就应该被选择
		$format_area = array();
		foreach($area_options as $area_id => $one_area){
			if(count( $one_area['cities']) && count( array_intersect($area, array_keys($one_area['cities']))) == count($one_area['cities'])) {
				$format_area[] = $area_id;
			} else {
				if(isset($one_area['cities'])) {
					$tmp_area = array_keys($one_area['cities']) + array($area_id);
				} else {
					$tmp_area = array($area_id);
				}
				$format_area += array_intersect($area, $tmp_area);
			}
		}
		
		$area_str = implode(',' , $format_area);
		$res = $campaignMdl->updateCampaignArea($this->me['user_id'], $campaign_id, $area_str);
		if($res) {
			$this->display_json( array( 'result' => true , 'area' => $res ) );
		}				
	}
			
	public function schedule()
	{
		$campaignMdl = new CampaignModel();
		$campaign_id = $this->_get('campaign_id');
		$td = array();
		for( $i = 1; $i < 49; $i++){
			$td[] = $i;
		}
		$tr = array('一','二','三','四','五' ,'六' ,'日');
		$this->tr = $tr;
		$this->td=$td;
		//	var timeDiscount = "00:00-24:00:125;00:00-24:00:100;00:00-24:00:100;00:00-24:00:100;00:00-24:00:100;00:00-24:00:100;00:00-24:00:100";
		$schedule = $campaignMdl->getCampaignSchedule($this->me['user_id'] , $campaign_id);
		$this->schedule=$schedule;
		$this->campaign_id = $campaign_id;
		$this->discount= implode(';' , $schedule);
		$this->display();
	}
	
	public function saveschedule()
	{
		$schedule = $_POST['schedule'];
		$campaign_id = $this->_post('campaign_id');
		$campaignMdl = new CampaignModel();
		$ret = $campaignMdl->saveCampaignSchedule($this->me['user_id'], $campaign_id, $schedule);
		$this->display_json( array( 'result' => true , 'schedule' => $schedule) );
	}
}
